// 定义一个函数用于获取格式化时间，接受一个可选的格式参数，默认格式为 'YYYY-MM-DD HH:mm:ss'
export const getFormattedTime = (
  date = new Date(),
  format = 'YYYY-MM-DD HH:mm:ss',
) => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0');
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');

  // 替换格式字符串中的占位符
  return format
    .replace('YYYY', year)
    .replace('MM', month)
    .replace('DD', day)
    .replace('HH', hours)
    .replace('mm', minutes)
    .replace('ss', seconds);
};

// 定义一个函数用于获取当前格式化时间，调用 getFormattedTime 函数并传入当前时间
export const getCurrentFormattedTime = (format = 'YYYY-MM-DD HH:mm:ss') => {
  return getFormattedTime(new Date(), format);
};

export const isInTimeRange = (startFormat, endFormat) => {
  const currentTime = getCurrentFormattedTime();
  const startTime = getFormattedTime(
    new Date(currentTime),
    'YYYY-MM-DD ' + startFormat,
  );
  const endTime = getFormattedTime(
    new Date(currentTime),
    'YYYY-MM-DD ' + endFormat,
  );

  const currentDate = new Date(currentTime).toDateString();
  const startTimeDate = new Date(startTime).toDateString();
  const endTimeDate = new Date(endTime).toDateString();

  if (currentDate === startTimeDate) {
    const currentHour = new Date(currentTime).getHours();
    const currentMinute = new Date(currentTime).getMinutes();
    if (currentHour === 10 && currentMinute >= 0 && currentMinute < 5) {
      return true;
    }
  } else if (currentDate === endTimeDate) {
    const currentHour = new Date(currentTime).getHours();
    const currentMinute = new Date(currentTime).getMinutes();
    if (currentHour === 10 && currentMinute > 0 && currentMinute <= 5) {
      return true;
    }
  } else {
    const currentDateObj = new Date(currentTime);
    const startTimeObj = new Date(startTime);
    const endTimeObj = new Date(endTime);
    if (currentDateObj >= startTimeObj && currentDateObj <= endTimeObj) {
      return true;
    }
  }

  return false;
};
